'use strict';

import Base from '../base.js';
function isEmpty(obj){
	return Object.keys(obj).length === 0 && obj.constructor === Object
}

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  indexAction(){
    //auto render template file index_index.html
    let data = {name: "thinkjs"};
    this.success(data);
  }
  //获取项目列表
  async activelistAction(){
  	let model = this.model('product');
  	let data = await model.select();
  	this.success(data);
  }
  //获取用户信息
  async getuserinfoAction(){
  	let formdata = this.post()
  	let nickname = formdata.nickname
  	if(!nickname){
  		this.error('该用户不存在');
  	}
  	let model = this.model('user');
  	let data = await model.where('nickname="'+nickname+'"').find();
  	this.success(data);
  }
   //获取项目信息
  async getproductinfoAction(){
  	let formdata = this.post()
  	let pid = formdata.pid
  	if(!pid){
  		this.error('项目页不存在');
  	}
  	let model = this.model('product');
  	let data = await model.where('id="'+pid+'"').find();
  	this.success(data);
  }
  //获取项目经理，技术经理列表
  async getteamleadersAction(){
  	let model = this.model('user');
  	let data = await model.where('isteamleader=1 or ispm=1').find();
  	this.success(data);
  }
  //score 给人打分
  async scoreAction(){
  	let formdata = this.post()
  	let pid = formdata.pid
  	let score = formdata.score
  	if(!pid || !score){
  		this.error();
  	}
  	let model = this.model('product');
  	let product_data = await model.where('id="'+pid+'"').find();
		if(isEmpty(product_data)){
			this.error('不存在存在该项目');
			return false;
		}
  	let affectedRows = await model.where({id: pid}).update({score: score});
  	this.success(affectedRows);
  }
  //score 给项目打分  项目经理给项目打分
  async scoreteamAction(){
  	let formdata = this.post()
  	let pid = formdata.pid
  	let teamid = formdata.teamid
  	let score = formdata.score
  	if(!pid || !score || !score){
  		this.error();
  	}
  	let model = this.model('product_team');
  	let product_data = await model.where('teamid="'+teamid+'" and pid="'+pid+'"').find();
		if(isEmpty(product_data)){
			let insertId = await model.add({teamid: teamid, pid: pid,score:score});
  		this.success('小组打分成功');
		}else{
			let affectedRows = await model.where('teamid="'+teamid+'" and pid="'+pid+'"').update({score: score});
  		this.success(affectedRows);
		}
  }
  //修改项目状态 更改项目状态
  async setproductinfoAction(){
  	let formdata = this.post()
  	let pid = formdata.pid
  	if(!pid){
  		this.error();
  	}
  	let title = formdata.data.title
  	let owner = formdata.data.owner
  	let productdescript = formdata.data.productdescript
    let manager = formdata.data.manager
    let teamleader = formdata.data.teamleader
    let productdate = formdata.data.productdate
    let status = formdata.data.status
  	let model = this.model('product');
  	let product_data = await model.where('id="'+pid+'"').find();
		if(isEmpty(product_data)){
			this.error('不存在存在该项目');
			return false;
		}
  	let affectedRows = await model.where('id="'+pid+'"').update({title: title, owner: owner,productdescript:productdescript,manager: 
  		manager,productdate:productdate ,productdate: productdate,status:status});
  	this.success('项目修改成功');
  }
  //修改项目状态
  async createproductAction(){
  	let formdata = this.post()
  	let title = formdata.title
  	let owner = formdata.owner
  	let productdescript = formdata.productdescript
    let manager = formdata.manager
    let teamleader = formdata.teamleader
    let productdate = formdata.productdate
  	let model = this.model('product');
  	let product_data = await model.where('title="'+title+'"').find();
  		if(!isEmpty(product_data)){
  			this.error('存在该项目');
  			return false;
  		}
  	let insertId = await model.add({title: title, owner: owner,productdescript:productdescript,manager: 
  		manager,productdate:productdate ,productdate: productdate});
  	this.success('项目创建成功');
  }
  //加入项目
  async joinproductAction(){
  	let postjson = this.post()
  	let uid = postjson.uid
  	let pid = postjson.pid
  	let usermodel = this.model('user');
  	let product_usermodel = this.model('product_user');
  	if(!pid){
  		this.error('项目信息不存在');
  		return false;
  	}
  	if(uid){
  		let user_data = await usermodel.where('id="'+uid+'"').find();
  		if(isEmpty(user_data)){
  			this.error('该用户不存在');
  			return false;
  		}
  		//直接加入
  		let product_user_data = await product_usermodel.where('pid="'+pid+'" and userid="'+uid+'"').find();
  		if(!isEmpty(product_user_data)){
  			this.error('该用户已经加入该项目');
  			return false;
  		}
  		let insertId = await product_usermodel.add({userid: uid, pid: pid});
			if(insertId){
				this.success('加入成功');
				return false;
			}else{
				this.error('用户加入项目失败');
				return false;
			}
  	}else{
  		//先创建用户
  		let formdata = postjson.data
  		if(!formdata){
  			this.error('用户创建数据错误');
  			return false;
  		}
  		let name = formdata.name
  		let team = formdata.team
  		let nickname = formdata.nickname
  		let data = await usermodel.where('nickname="'+nickname+'"').find();
  		if(!isEmpty(data)){
  			this.error('该用户已经存在');
  			return false;
  		}
  		let product_user_data = await product_usermodel.where('pid="'+pid+'" and userid="'+uid+'"').find();
  		if(!isEmpty(product_user_data)){
  			this.error('该用户已经加入该项目');
  			return false;
  		}
  		let insertUser = await usermodel.add({name: name, team: team,nickname:nickname});
  		if(insertUser){
  			let insertId = await product_usermodel.add({userid: insertUser, pid: pid});
  			if(insertId){
  				this.success('加入成功');
  				return false;
  			}else{
  				this.error('用户加入项目失败');
  				return false;
  			}
  		}else{
  			this.error('用户创建失败');
  			return false;
  		}
  	}
  }
}